﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wdsDeApp03.Models;

namespace wdsDeApp03.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(string type = null, string text = null)
        {
            using (Model1 model = new Model1())
            {
                #region 获取信息
                var info = model.ProInfo.Where(u => true).ToList();
                if (int.TryParse(type, out int i))
                {
                    var typeId = Convert.ToInt32(type);
                    if (typeId != 0)
                        info = info.Where(u => u.Type == typeId).ToList();
                }
                if (!string.IsNullOrEmpty(text))
                    info = info.Where(u => u.Name.IndexOf(text) >= 0).ToList();
                #endregion

                #region 获取类型
                Dictionary<int, string> keyValuePairs = new Dictionary<int, string>();
                var typeInfo = model.ProType.Where(u => true).ToList();
                foreach (var item in typeInfo)
                    keyValuePairs.Add(item.ID, item.Name);
                ViewBag.typeInfo = keyValuePairs;
                #endregion

                #region 获取类型下拉框
                List<SelectListItem> selectListItems = new List<SelectListItem>();
                selectListItems.Add(new SelectListItem() { Value = "0", Text = "全部" });
                foreach (var item in typeInfo)
                    selectListItems.Add(new SelectListItem() { Value = item.ID.ToString(), Text = item.Name.ToString() });
                ViewBag.dropDownList = new SelectList(selectListItems, "Value", "Text");
                #endregion

                return View(info);

            }
        }

        [HttpGet]
        public ActionResult Create()
        {
            using (Model1 model = new Model1())
            {
                #region 下拉框
                var typeInfo = model.ProType.Where(u => true).ToList();
                List<SelectListItem> selectListItems = new List<SelectListItem>();
                foreach (var item in typeInfo)
                    selectListItems.Add(new SelectListItem() { Value = item.ID.ToString(), Text = item.Name.ToString() });
                ViewBag.dropDownList = new SelectList(selectListItems, "Value", "Text");
                #endregion

                return View(new ProInfo());
            }

        }

        [HttpPost]
        public ActionResult Create(ProInfo proInfo)
        {
            if (ModelState.IsValid) {
                using (Model1 model = new Model1())
                {
                    model.ProInfo.Add(proInfo);
                    if (model.SaveChanges() == 1)
                        return RedirectToAction("Index", "Home");
                    else
                        return Json(false);
                }
            }
            return Json(false);
        }

        [HttpPost]
        public ActionResult Update(string uid)
        {
            if (int.TryParse(uid, out int i))
            {
                using (Model1 model = new Model1())
                {
                    var uId = Convert.ToInt32(uid);
                    model.ProInfo.FirstOrDefault(u => u.ID == uId).IsDestroy = true;

                    return Json(model.SaveChanges());
                }
            }
            return Json(2);
        }
    }
}